import { defineStore } from "pinia";
import { requestCategoryList1, requestCategoryList2, requestCategoryList3 } from '@/api/attr'
import type { Category1ReturnType, Category2ReturnType } from '@/api/attr'
import { ElMessage } from "element-plus";
interface storeType {
  categoryList1: Category1ReturnType,
  categoryList2: Category2ReturnType,
  categoryList3: Category2ReturnType,
  category1Id: number | null,
  category2Id: number | null,
  category3Id: number | null,
}
const useAttrStore = defineStore('attr', {
  state: (): storeType => {
    return {
      categoryList1: [],
      categoryList2: [],
      categoryList3: [],
      category1Id: null,
      category2Id: null,
      category3Id: null,
    }
  },
  getters: {},
  actions: {
    //1.获取一级列表
    async getCategorylist1() {
      try {
        const result = await requestCategoryList1()
        this.categoryList1 = result
      } catch (e) {
        ElMessage.error('获取一级列表错误')
      }
    },
    //2.获取二级列表
    async getCategorylist2() {
      try {
        const result = await requestCategoryList2(this.category1Id as number)
        this.categoryList2 = result
      } catch (e) {
        ElMessage.error('获取二级列表错误')
      }
    },
     //2.获取三级列表
     async getCategorylist3() {
      try {
        const result = await requestCategoryList3(this.category2Id as number)
        this.categoryList3 = result
      } catch (e) {
        ElMessage.error('获取三级列表错误')
      }
    }
  }
})

export default useAttrStore